package com.lh.web.controller.shop;

import com.lh.common.entity.admin.Column;
import com.lh.common.entity.admin.Shop;
import com.lh.common.enums.ColumnTypeEnum;
import com.lh.common.enums.UpperLowerShelfStatus;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.service.serviceAdmin.shop.ColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ColumnAction
 * @Description: (栏目管理)
 * @date: 2020-06-29 10:57
 */
@Api(value="ColumnAction",tags={"栏目管理"})
@RestController
@RequestMapping("column")
public class ColumnAction {

    @Autowired
    ColumnService columnService;

    /**
     * 查询栏目管理分页
     */
    @ApiOperation(value = "查询栏目管理分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "栏目名称", required = false, dataTypeClass= String.class)
    })
    @GetMapping("/getColumnList")
//    @RequiresPermissions("column:getColumnList")
    public Result<Column> getColumnList(String name, Integer pageNum, Integer pageSize) {
        pageNum = pageNum== null ? 1:pageNum;
        pageSize = pageSize== null ? Integer.MAX_VALUE:pageSize;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("name", name);
        PageUtils<Column> shopMyPage = columnService.queryColumnList(params,pageNum,pageSize);
        return Result.ok(shopMyPage);
    }

    /**
     * 新增/修改栏目"
     */
    @ApiOperation(value = "新增/修改栏目")
    @PostMapping("/setColumn")
//    @RequiresPermissions("column:setColumn")
    public Result<Column> setColumn(@RequestBody Column column) {
        AssertUtil.assertNotNull(column.getName(), Constant.RESULT.FI1000, "栏目名称为空!");
        AssertUtil.assertNotNull(column.getColumnPic(), Constant.RESULT.FI1000, "栏目图片为空!");
        AssertUtil.assertNotNull(column.getType(), Constant.RESULT.FI1000, "栏目类型为空!");
        Column columnName = columnService.getColumnName(column.getName());
        if(column.getId() == null) {
            if(columnName != null){
                return Result.error("类目名称已存在!");
            }
            columnService.addColumn(column);
            return Result.ok();
        }
        AssertUtil.assertNotNull(column.getId(), Constant.RESULT.FI1000, "类目ID为空!");
        columnService.updateColumn(column);
        return Result.ok();
    }

    @ApiOperation(value = "栏目上下架/排序")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id", value = "id", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "sort", value = "排序", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态 UPPERSHELF:显示; LOWERSHELF:不显示", required = false, dataTypeClass = UpperLowerShelfStatus.class)
    })
    @ResponseBody
    @PostMapping("/updateColumnStatus")
//    @RequiresPermissions("column:updateColumnStatus")
    public Result updateColumnStatus(@RequestBody Column column) {
        AssertUtil.assertNotNull(column.getId(), Constant.RESULT.FI1000, "类目ID为空!");
        if(column.getStatus() != null && column.getStatus().equals(UpperLowerShelfStatus.UPPERSHELF)){
            int i = columnService.queryColumnCount();
            if( i >= 10) return Result.error("已超过显示上限！");
        }
        columnService.updateColumnStatus(column);
        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "栏目ID", required = true)
    })
    @GetMapping("/deleteById")
    @ResponseBody
//    @RequiresPermissions("column:deleteById")
    public Result del(Long id){
        try {
            columnService.deleteColumn(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除分类异常!");
        }
    }

    /**
     * 查询栏目详情
     */
    @ApiOperation(value = "查询栏目详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "栏目ID", required = false, dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "type", value = "类型", required = false, dataTypeClass= ColumnTypeEnum.class),
    })
    @GetMapping("/getColumnDetails")
//    @RequiresPermissions("column:getColumnDetails")
    public Result<Column> getColumnDetails(Long id,ColumnTypeEnum type) {
        Column columnDetails = columnService.getColumnDetails(id, type);
        return Result.ok(columnDetails);
    }
}
